import 'package:flutter/material.dart';

import '../../core/utils/show_toast.dart';
import '../../data/controllers/additional_operations_controller.dart';
import '../../presentation/additional_operations_page/bloc/dynamic_ui_cubit.dart';
import '../../presentation/additional_operations_page/bloc/dynamic_ui_screen.dart';


class UiEvents{
  final int type;
  final String widgetKey;
  final String title;
  final String route;
  final Map<String , dynamic> data;
  final UiEvents? nextEvent;

  UiEvents({
    required this.type,
    required this.widgetKey,
    required this.title,
    required this.route,
    required this.data,
    this.nextEvent,
  });

  run({AdditionalOperationsCubit? cubit,required BuildContext context})async{
    switch (type){

      case 0:
        // none
        break;
      case 1:
        // go to new dynamic screen
        Navigator.push(context, MaterialPageRoute(builder: (context) => DynamicUiScreen(route: route)));
        break;
      case 2:
        // get data from server
        await AdditionalOperationsController.getData(route);
        break;
      case 3:
        // post data to server
        await AdditionalOperationsController.postData(from: route,data: data);
        break;
      case 4:
        // show message
        if(data['message'] != null) {
          showMessage(data['message']);
        }else{
          showErrorMessage(message: 'الحدث لا يحوي رسالة لعرضها');
        }
        break;

      case 5:
        break;

      case 6:
        break;

      case 7:
        break;
      case 8:
        break;

      default:
        showErrorMessage(message: 'لم يتم التعرف على الحدث');


    }
    nextEvent?.run(context:context,cubit: cubit);
  }




  factory UiEvents.fromJson(Map<String, dynamic> json) => UiEvents(
    type: json["type"]??-1,
    widgetKey: json["widget_key"]??"",
    title: json["title"]??"",
    route: json["route"]??"",
    data: json["data"]??{},
    nextEvent: (json["nextEvent"]!= null)?UiEvents.fromJson(json["nextEvent"]): null,
  );

  Map<String, dynamic> toJson() => {
    "type": type,
    "widget_key": widgetKey,
    "data": data,
    "route": route,
    "next_event": nextEvent,
    "title":title
  };
}